package com.yunkaweilai.android.view.dialog.room;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.lazylibrary.util.StringUtils;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.operation.consumption.room.RoomInfoActivity;
import com.yunkaweilai.android.base.BaseApplication;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.model.GuadanRoomModel;
import com.yunkaweilai.android.model.PayTypeModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.MyToast;

/**
 * 开台
 * Created by Administrator on 2016/12/22 0022.
 */

public class OpenRoomDialog extends Dialog {
    ImageView idImgClose;
    private TextView idTvClose;
    private TextView idTvRight;
    private String name;
    private String remark;
    private TextView idTvName;
    private TextView idTvRemark;
    private String id;
    private LinearLayout idLlayoutLable;
    private PayTypeModel payModel;
    private String LabelId = "0";
    private String isFangfei;
    private EditText idEdtRoomPrice;
    private LinearLayout idLlayoutFangfei;
    private TextView idTvLableUpdate;
    private TextView idTvLable;
    private OpenRoomDialog roomDialog;
    private Context context;

    public OpenRoomDialog(Context context,String name, String remark, String id,String isFangfei) {
        super(context,R.style.dialog);
        this.context = context;
        this.id = id;
        this.name = name;
        this.remark = remark;
        this.isFangfei = isFangfei;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_open_room);
        roomDialog = this;
        idLlayoutLable = ((LinearLayout)findViewById(R.id.id_llayout_lable));
        idTvLableUpdate = ((TextView) findViewById(R.id.id_tv_lable_update));

        payModel = BaseApplication.getPayModel();
        if ("1".equals(payModel.getData().getInfo().getIs_guadan_label())){
            idLlayoutLable.setVisibility(View.VISIBLE);
        }else{
            idLlayoutLable.setVisibility(View.GONE);
        }
        if ("1".equals(payModel.getData().getInfo().getIs_guadan_label())){
            idTvLableUpdate.setVisibility(View.VISIBLE);
        }else{
            idTvLableUpdate.setVisibility(View.GONE);
        }
        idLlayoutFangfei = ((LinearLayout) findViewById(R.id.id_llayout_fangfei));
        if ("1".equals(isFangfei)){
            idLlayoutFangfei.setVisibility(View.VISIBLE);
        }else{
            idLlayoutFangfei.setVisibility(View.GONE);
        }
        idTvLable = ((TextView)findViewById(R.id.id_tv_lable));
        idImgClose = (ImageView)findViewById(R.id.id_img_close);
        idTvRight = ((TextView) findViewById(R.id.id_tv_right));
        idTvClose = (TextView) findViewById(R.id.id_tv_close);
        idTvName = ((TextView) findViewById(R.id.id_tv_name));
        idEdtRoomPrice = ((EditText) findViewById(R.id.id_edt_room_price));
        idTvName.setText(name);
        idTvRemark = ((TextView) findViewById(R.id.id_tv_remarks));
        idTvRemark.setText(remark);

        initclick();
    }


    public void initclick() {
        idImgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        idTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("1".equals(isFangfei)){
                    if(StringUtils.isEmpty(idEdtRoomPrice.getText().toString())){
                        MyToast.showShortToast(context,"还未输入房费！");
                    }else{
                        initHttp();
                        dismiss();
                    }
                }else{
                    initHttp();
                    dismiss();
                }
            }
        });
        idTvClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        idTvLableUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GuadanRoomLableDialog dialog = new GuadanRoomLableDialog(context,roomDialog);
                dialog.show();
            }
        });
    }

    private void initHttp() {
        HttpRequest.postUrl(Api.GUADAN_ROOM_OPEN)
                .addParams("Group_id",id)
                .addParams("LabelId",LabelId)
                .addParams("fangfei", idEdtRoomPrice.getText().toString())
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(context, json);
                        if (checkCode){
                            Gson gson = new Gson();
                            GuadanRoomModel guadanRoomModel = gson.fromJson(json, GuadanRoomModel.class);
                            RoomInfoActivity.actionStart(context,guadanRoomModel.getData().getGid(),name,true);
                        }
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                });
    }

    public void selectedLable(String name,String id){
        LabelId = id;
        idTvLable.setText(name+"");
    }

}
